package com.api.salary.listener;

import com.sun.jersey.api.model.AbstractResourceModelContext;
import com.sun.jersey.api.model.AbstractResourceModelListener;
import com.weaver.upgrade.FunctionUpgradeUtil;
import org.apache.commons.lang3.StringUtils;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.system.License;

import javax.ws.rs.ext.Provider;

@Provider
public class InitListener implements AbstractResourceModelListener {
    @Override
    public void onLoaded(AbstractResourceModelContext abstractResourceModelContext) {
        int menuid = 100118;
        BaseBean bb = new BaseBean();
        String cId = new License().getCId();
        String defaultCloseNonStandard149 = bb.getPropValue("hrmSalary", "defaultCloseNonStandard149");
        if (StringUtils.isNotBlank(cId) && "true".equals(defaultCloseNonStandard149)) {
            if (cId.length() > 7) {
                cId = cId.substring(cId.length() - 7);
            }
            int cid = Integer.parseInt(cId);
            if (!hasMenuInfo(cid, "left", menuid)) {
                bb.writeLog("149新薪酬管理 添加默认菜单控制");
                insertMenuInfo(cid, "left", menuid);
            }
        }
    }

    private void insertMenuInfo(int cid, String type, int id) {
        RecordSet rs = new RecordSet();
        BaseBean bb = new BaseBean();
        bb.writeLog("FunctionUpgradeUtil.getMenuId(cid,id):" + FunctionUpgradeUtil.getMenuId(cid, id));
        String sql = "INSERT  INTO  menucontrollist  (type,menuid,isopen)  VALUES('" + type + "','" + FunctionUpgradeUtil.getMenuId(cid, id) + "','" + FunctionUpgradeUtil.getMenuStatus(id, -1, cid) + "')";
        rs.execute(sql);
    }

    private boolean hasMenuInfo(int cid, String type, int id) {
        RecordSet rs = new RecordSet();
        String sql = "select count(1) as count from menucontrollist where type = ? and menuid = ?";
        rs.executeQuery(sql, type, FunctionUpgradeUtil.getMenuId(cid, id));
        if (rs.next()) {
            int count = rs.getInt("count");
            if (count > 0) {
                return true;
            }
        }
        return false;
    }
}
